package lion.framework.core.web.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;
import lion.framework.core.web.views.IView;

/**
 * 异常视图
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年3月18日
 */
public class ExceptionView implements IView {

	public static final String VIEW_TYPE = "exception";
	private WebException exception;
	private String name;

	public ExceptionView(Throwable e) {

		if (e == null) { return; }
		if (e instanceof WebException) {
			this.exception = (WebException) e;
		} else {
			this.exception = new WebException(500, e);
		}
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) {

		String path = request.getRequestURI().substring(request.getContextPath().length());
		IExceptionHandler handler = WEBExceptionHandlers.getExceptionHandler(path);
		Config config = ConfigManager.getConfig("framework");
		response.setContentType("text/html;charset=" + config.getString("framework.sysencoding", "UTF-8"));
		response.setStatus(exception.getStatus());
		try {
			handler.handler(request, response, this.exception);
		} catch (Exception e) {
		}
	}

	@Override
	public String getName() {

		return this.name;
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

}
